package toolkit.dart.porting.actions;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IEditorActionDelegate;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.texteditor.AbstractTextEditor;

public abstract class TextSelectionActionDelegate implements IEditorActionDelegate{

	protected IEditorPart activeEditor;

	public TextSelectionActionDelegate() {
		super();
	}

	@Override
	public void run(IAction arg0) {
	    if (activeEditor instanceof AbstractTextEditor) {
	        ISelection selection = ((AbstractTextEditor)activeEditor).getSelectionProvider().getSelection();
	        if (selection instanceof ITextSelection)
	        {
	            ITextSelection textSel = (ITextSelection) selection;
	            runForText(textSel.getText());
	        }
	    }
	}

	protected abstract void runForText(String text);

	@Override
	public void selectionChanged(IAction action, ISelection selection) {
	    if (selection instanceof ITextSelection) {
	        action.setEnabled(((ITextSelection)selection).getLength() > 0);
	    }
	}

	@Override
	public void setActiveEditor(IAction action, IEditorPart part) {
	    activeEditor = part;
	}

}